﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class SocketConnectionsManager
    {
        private List<ClientSocketData> clientSockets = new List<ClientSocketData>();
        private List<ServerSocketData> serverSockets = new List<ServerSocketData>();

        public SocketConnectionsManager() { }

        public void CloseAllServerConnections()
        {
            lock (serverSockets)
            {
                while (serverSockets.Count > 0)
                {
                    serverSockets[0].socket.Close();
                    serverSockets.RemoveAt(0);
                }
            }
        }
        public void CloseAllClientConnections()
        {
            lock (clientSockets)
            {
                while (clientSockets.Count > 0)
                {
                    clientSockets[0].socket.Close();
                    clientSockets.RemoveAt(0);
                }
            }
        }
        public bool ContainsServerSocket(ServerSocketData ssd)
        {
            bool result = false;
            lock (serverSockets)
            {
                if (serverSockets.Contains(ssd))
                    result = true;
            }
            return result;
        }
        public void AddServerSocket(ServerSocketData ssd)
        {
            lock (serverSockets)
            {
                serverSockets.Add(ssd);
            }
        }
        public void RemoveServerSocket(ServerSocketData ssd)
        {
            lock (serverSockets)
            {
                serverSockets.Remove(ssd);
            }
        }
        public int CountServerSockets()
        {
            int result = 0;
            lock (serverSockets)
            {
                result = serverSockets.Count;
            }
            return result;
        }
        public List<ServerSocketData> GetServerSocketsCpy()
        {
            List<ServerSocketData> result = new List<ServerSocketData>();
            lock (serverSockets)
            {
                foreach (ServerSocketData ssd in serverSockets)
                    result.Add(ssd);
            }
            return result;
        }

        public void AddClientSocket(ClientSocketData csd)
        {
            lock (clientSockets)
            {
                clientSockets.Add(csd);
            }
        }
        public void RemoveClientSocket(ClientSocketData csd)
        {
            lock (clientSockets)
            {
                clientSockets.Remove(csd);
            }
        }
        public int CountClientSockets()
        {
            int result = 0;
            lock (clientSockets)
            {
                result = clientSockets.Count;
            }
            return result;
        }
        public List<ClientSocketData> GetClientSocketsCpy()
        {
            List<ClientSocketData> result = new List<ClientSocketData>();
            lock (clientSockets)
            {
                foreach (ClientSocketData csd in clientSockets)
                    result.Add(csd);
            }
            return result;
        }
        public bool ClientExists(int UserID)
        {
            lock (clientSockets)
            {
                foreach (ClientSocketData csd in clientSockets)
                {
                    if (csd.UserID == UserID)
                        return true;
                }
            }
            return false;
        }
        public bool ClientExists(string clientID)
        {
            lock (clientSockets)
            {
                foreach (ClientSocketData csd in clientSockets)
                {
                    if (csd.clientID == clientID)
                        return true;
                }
            }
            return false;
        }
        static public bool sendToServer(ServerSocketData ssd, string XMLMessage)
        {
            try
            {
                byte[] byData = System.Text.Encoding.ASCII.GetBytes(XMLMessage + "\0");
                ssd.socket.Send(byData);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public void sendToAllServersExcluding(ServerSocketData excludeServer, string XMLMessage)
        {
            lock (serverSockets)
            {
                foreach (ServerSocketData server in serverSockets)
                {
                    if (server != excludeServer)
                        sendToServer(server, XMLMessage);
                }
            }
        }

    }
}
